package material.encapsulation_algorithm.sort;

/**
 * 我们之所以需要让鸭子类实现Compareable接口，因为我们无法真的让鸭子数组去继承数组。
 */
public class Duck implements Comparable {
    String name;
    int weight;

    public Duck(String name, int weight) {
        this.name = name;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return name + " 重量:" + weight;
    }

    /**
     * compareTo需要被传入另-鸭子。和本身这只鸭子做比较。
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(Object o) {
        Duck otherDuck = (Duck) o;
        if (this.weight < otherDuck.weight) {
            return -1;
        } else if (this.weight == otherDuck.weight) {
            return 0;
        } else {
            return 1;
        }
    }
}
